package cn.school.service;

import cn.school.dao.StudentMapper;
import cn.school.entity.Student;
import cn.school.util.MybatisUtil;
import cn.school.vo.AjaxPage;
import cn.school.vo.Page;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.session.SqlSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;
import java.util.List;

public class StudentService {
    private static Logger Log=LoggerFactory.getLogger(StudentService.class);

    public static final StudentService INST=new StudentService();

    public boolean checkPhone(String phone){
        SqlSession s= MybatisUtil.get();
        try {
            StudentMapper mapper = s.getMapper(StudentMapper.class);
            return mapper.checkPhone(phone)>0;
        }finally {
            MybatisUtil.close(s);
        }
    }

    public Student get(long stuno){
        SqlSession s= MybatisUtil.get();
        try {
            StudentMapper mapper = s.getMapper(StudentMapper.class);
            return mapper.get(stuno);
        }finally {
            MybatisUtil.close(s);
        }
    }
    public int update(Student stu){
        SqlSession s= MybatisUtil.get();
        try {
            StudentMapper mapper = s.getMapper(StudentMapper.class);
            return mapper.update(stu);
        }finally {
            MybatisUtil.close(s);
        }
    }
    public int add(Student stu){
        SqlSession s= MybatisUtil.get();
        try {
            StudentMapper mapper = s.getMapper(StudentMapper.class);
            return mapper.add(stu);
        }catch (Exception e){
            Log.error("增加失败",e);
            return 0;
        }finally {
            MybatisUtil.close(s);
        }
    }

    /**
     * 修改密码。
     * arg0,arg1,arg2,param0,param1...9
     *
     * @param stuno
     * @param newPass
     * @return
     */
    public int updatePass( long stuno,  String newPass){
        SqlSession s= MybatisUtil.get();
        try {
            StudentMapper mapper = s.getMapper(StudentMapper.class);
            return mapper.updatePass(stuno,newPass);
        }finally {
            MybatisUtil.close(s);
        }
    }
    public Page<Student> search(int pageNo, int pageSize, String sname, String qq, String phone, Date enterStart, Date enterEnd) {
        SqlSession s = MybatisUtil.get();
        try {
            StudentMapper mapper = s.getMapper(StudentMapper.class);
            List<Student> search = mapper.search(pageNo, pageSize, sname, qq, phone, enterStart, enterEnd);
            int total = mapper.searchTotal(sname, qq, phone, enterStart, enterEnd);
            return new Page<Student>(search,pageNo,total);
        } finally {
            MybatisUtil.close(s);
        }
    }
    public AjaxPage search2(int pageNo, int pageSize, String sname, String qq, String phone, Date enterStart, Date enterEnd) {
        SqlSession s = MybatisUtil.get();
        try {
            StudentMapper mapper = s.getMapper(StudentMapper.class);
            List<Student> search = mapper.search(pageNo, pageSize, sname, qq, phone, enterStart, enterEnd);
            int total = mapper.searchTotal(sname, qq, phone, enterStart, enterEnd);
            return new AjaxPage(search,total,pageSize,pageNo);
        } finally {
            MybatisUtil.close(s);
        }
    }
//    public List<Student> search(  int pageNo,  int pageSize,  String sname,  String qq, String phone, Date enterStart,  Date enterEnd){
//        SqlSession s= MybatisUtil.get();
//        try {
//            StudentMapper mapper = s.getMapper(StudentMapper.class);
//            return mapper.search(pageNo, pageSize, sname, qq, phone, enterStart, enterEnd);
//        }finally {
//            MybatisUtil.close(s);
//        }
//    }
//
//    public int searchTotal(  String sname,  String qq,   String phone,   Date enterStart,   Date enterEnd){
//        SqlSession s= MybatisUtil.get();
//        try {
//            StudentMapper mapper = s.getMapper(StudentMapper.class);
//            return mapper.searchTotal( sname, qq, phone, enterStart, enterEnd);
//        }finally {
//            MybatisUtil.close(s);
//        }
//    }
 }
